package com.diaohaiwen.basic.websockt.service;

import com.diaohaiwen.basic.websockt.mapper.NoticeMapper;
import com.diaohaiwen.basic.websockt.obj.bo.FileBo;
import com.diaohaiwen.basic.websockt.obj.model.Notice;
import com.diaohaiwen.framework.web.util.RequestUtil;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;

/**
 * @author 刁海文
 * @since 2023/12/9 23:04
 */
@Service
public class FileService {
    @Resource
    NoticeMapper noticeMapper;

    @SneakyThrows
    public void download(String noticeId) {
        Notice notice = noticeMapper.selectOneById(noticeId);
        FileBo file = notice.file();
        FileInputStream stream = new FileInputStream(new File(file.getFolder(), file.getFilename()));
        RequestUtil.download(stream, file.contentType, file.getOriginal());
    }
}
